while
¶Learning Objectives
while
syntaxnot
syntaxwhile
and decompositionfix_tree.py
NOTES
A woman and her husband were standing in the Lourve observing the Mono Lisa. The husband turned to his wife and commented: "I don't see what so great about this painting." In response, the musuem guard standing nearby leaned over and replied: "Ah! But don't you wish you could!"
The ability to behold beauty, creativity, and talent is a gift. In a domain that is new to us, we may sometimes feel like the husband in this story: failing to appreciate what is in front of us.
At first, you may have a similar experience as you learn how to program. There are many ways to write a program, but some are better than others. You might not appreciate the intelligence behind the differences at first, but as you practice and study, the differences will become more clear. You will learn the patterns, intuitions, and techniques that make programming enjoyable and powerful.
Then, not only will you be able to appreciate the competent work of others, you will be able to create intelligent, beautiful code of your own.
Writing code is like painting.
There are techniques to learn, like color-mixing, brush strokes, layering, shading, perspective, etc.
There is also creative intuition: the ability to exercise the techniques you know to create something beautify.
To build beautiful code, you'll need to know the techniques, and you'll need to develop creative intuition.
Over the next few days, we'll talk about important techniques and practice, practice, practice!
Consider the following task: fill the bottom row of a bit world with green.
Bit has a method for checking whether the front of Bit is clear (i.e. a move will not go out of bounds).
bit.front_clear()
while
¶go_green.py
¶NOTES
Step through the code, one step at a time.
Point out how the loop condition is checked before each run of the body.
Show how the loop body is skipped when the condition is False
.
If necessary, change the last line to bit.paint('blue')
to clarify that the body is completely skipped when the condition is false.
Follow a similar approach for each of the demonstrations in this lesson.
while <condition>:
if the condition was True
run these lines
then check the condition again
Repeating the same lines of code is called a loop.
A loop body contains the instructions we want to run multiple times.
The loop condition determines whether the loop body will be run.
The loop body is indented 4 spaces relative to the while
statement.
from byubit import Bit
@Bit.run_from_empty(5, 3)
def go_green(bit):
while bit.front_clear():
bit.paint('green')
bit.move()
bit.paint('blue')
A condition is something that is either true or false.
We can represent the concept of true or false in python using the values True
and False
.
True
False
These are booleans (named after George Boole; you can look him up). In python the type is called bool
type(True)
Consider the following
from byubit import Bit
@Bit.run_from_empty(5, 3)
def go_green(bit):
while bit.front_clear():
bit.paint('green')
bit.move()
Why wasn't the last square painted?
from byubit import Bit
@Bit.run_from_empty(5, 3)
def go_green(bit):
while bit.front_clear():
bit.move()
bit.paint('green')
When using while
loops, always consider the boundary conditions.
A boundary condition is the state of your program when a loop starts or finishes.
For example:
while
loop, be clear about the boundary conditions.
Sometimes a Bit world includes black squares. These squares are blocked; Bit cannot move onto them.
from byubit import Bit
@Bit.run('blocked')
def blocked(bit):
bit.move()
Besides front_clear()
, Bit can also check left_clear()
and right_clear()
.
not
¶You can turn a True
to False
and a False
to True
using the not
keyword.
NOTES
Point out how right_clear()
returns False
, but not
turns it into True
.
Bit can tell you what color it is sitting on.
bit.is_blue()
bit.is_green()
bit.is_red()
bit.is_empty()
Move Bit to the blue square.
while True
¶What happens when the condition of a while
loop is always True
?
infinite_loop.py
¶NOTES
Bit has an "infinite loop detection" feature, so if you make a mistake and create an infinite loop, it won't be too much of a hassle.
Getting stuck in an infinite loop is not problem (your computer will not explode), only an inconvenience. Even experienced programmers still run into them from time to time.
Also, not all infinite loops are mistakes. Sometimes you actually do want a loop to run forever (or close to it).
Let's paint something.
fix_tree.py
¶Bit has a nice tree in his yard, but it is missing its trunk!
Help Bit fix the tree by adding the trunk.
NOTES
Make sure to draw this out. Demonstrate how the students can use drawing to grasp the nature of the problem and design a strategy.
Demonstrate how drawing can be used to clarify boundary conditions.
Give the students time to discuss together (in pairs) how they would break the problem up.
Demonstrate the process of going from abstract stages to concrete, technical requirements. For example, getting to the red square is an abstract stage. Deciding that you need a while
loop with an looping condition of not bit.is_red()
and a body with bit.move()
but no painting are the technical requirements.
Where will the state be after the first stage completes? Where will Bit be? What direction will he be facing?
Get Bit to the red square
NOTES
What is the condition? bit.is_red()
? bit.is_empty()
? not bit.is_red()
?
What if there were little green bushes in the way? Would bit.is_empty()
still work?
What if the base of the tree were blue? Would not bit.is_red()
still work?
There are many ways to solve a problem. If the nature of the problem changes, even a little, you will likely need to adapt your code (e.g. change bit.is_empty()
to not bit.is_red()
). This is a natural, essential part of the programming experience.
Fill in the trunk
NOTES
What is the loop condition?
Try first:
bit.move()
bit.paint('red')
Use the stepper to back off the error and observe where the problem occurred.
Observe how the paint('red')
clobbers the square and colludes with the loop condition.
Note how the loop body and condition interact. Planning out and thinking through this interaction is important.
With move-then-paint, do you move first to get off the original red square, or do you simply repaint it?
Sometimes it is more convenient to repaint a square or two. That's fine. But avoid recless repainting (i.e. inserting paint statements all over your code) as a workaround to poorly-understood boundary conditions.
Use the snapshot feature to show how the students can coordinate stages of their solution with positions in their code.
Finally, show how the code works on both problems (switch between the two tabs).
while
not
New Bit methods
bit.front_clear()
bit.left_clear()
bit.right_clear()
bit.is_blue()
bit.is_green()
bit.is_red()
bit.is_empty()